package com.zbc.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 腾讯云 COS 配置
 * @author fangyu
 * @version v1.0.0
 * @since 2025/9/2 13:53
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "cos.client")
public class TencentCosClient {

    /**
     * 域名
     */
    private String host;

    /**
     * secretId
     */
    private String secretId;

    /**
     * secretKey
     */
    private String secretKey;

    /**
     * 地域
     */
    private String region;

    /**
     * 存储桶名称
     */
    private String bucket;

    /**
     * 创建 COS 客户端
     *
     * @return COSClient
     */
    @Bean
    public COSClient cosClient() {
        // 1. 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2. 设置bucket的地域
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        // 可选: 设置使用https(5.6.54版本之后默认是https)
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3. 生成cosClient客户端
        return new COSClient(cred, clientConfig);
    }
}
